package org.eleorc.resources.builtinresource;

import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
import java.net.URL;

public class JarResourceAccessExample {

    public static void main(String[] args) {
        // 假设有一个名为app-config.xml的配置文件被打包在项目的JAR文件内
        String resourceName = "META-INF/spring.tooling";

        try {
            // 创建ClassPathResource实例
            ClassPathResource resource = new ClassPathResource(resourceName);

            // 检查资源是否存在
            if (resource.exists()) {
                System.out.println("资源存在于类路径中");

                // 通过getResourceURL()方法获取资源的URL
                URL resourceUrl = resource.getURL();
                System.out.println("资源URL: " + resourceUrl);

                // 通过getInputStream()方法获取资源的输入流
                try (java.io.InputStream inputStream = resource.getInputStream()) {
                    // 读取资源内容
                    byte[] buffer = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = inputStream.read(buffer)) != -1) {
                        System.out.print(new String(buffer, 0, bytesRead));
                    }
                }

            } else {
                System.out.println("资源不存在");
            }
        } catch (IOException e) {
            System.err.println("访问资源时发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
